/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */

package com.nai365.nart.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.log.annotation.SysLog;
import com.nai365.nart.common.security.annotation.Inner;
import com.nai365.nart.common.security.util.SecurityUtils;
import com.nai365.nart.member.dto.LatestVersion;
import com.nai365.nart.member.entity.App;
import com.nai365.nart.member.service.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * app管理
 *
 * @author linco
 * @date 2022-07-07 11:36:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/app")
@Api(value = "app", tags = "App版本管理")
public class AppController {

	private final AppService appService;

	/**
	 * 分页查询
	 *
	 * @param page 分页对象
	 * @param app  app管理
	 * @return R<Page < App>>
	 */
	@ApiOperation(value = "App版本分页查询", tags = {"App版本管理-PC端"})
	@GetMapping("/page")
	public R<Page<App>> getAppPage(Page<App> page, App app) {
		return R.ok(appService.page(page, Wrappers.query(app)));
	}


	/**
	 * 通过id查询app管理
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "App版详情", tags = {"App版本管理-PC端"})
	@GetMapping("/{id}")
	public R<App> getById(@PathVariable("id") Long id) {
		return R.ok(appService.getById(id));
	}

	/**
	 * 新增app管理
	 *
	 * @param app app管理
	 * @return R
	 */
	@ApiOperation(value = "新增App版本", tags = {"App版本管理-PC端"})
	@SysLog("新增app管理")
	@PostMapping
	public R<App> save(@RequestBody App app) {
		app.setId(null);
		app.setCreateBy(SecurityUtils.getUser().getId());
		app.setUpdateTime(LocalDateTime.now());
		appService.save(app);
		return R.ok();
	}

	/**
	 * 修改app管理
	 *
	 * @param app app管理
	 * @return R
	 */
	@ApiOperation(value = "修改App版本", tags = {"App版本管理-PC端"})
	@SysLog("修改app管理")
	@PutMapping
	public R<App> updateById(@RequestBody App app) {
		app.setUpdateTime(LocalDateTime.now());
		app.setCreateBy(SecurityUtils.getUser().getId());
		appService.updateById(app);
		return R.ok(app);
	}

	/**
	 * 修改app管理
	 *
	 * @param versionName 版本名称
	 * @return R
	 */
	@ApiOperation(value = "新版本检测", tags = {"App版本管理-移动端"})
	@SysLog("修改app管理")
	@GetMapping("/check_update")
	@Inner(value = false)
	public R<App> checkUpdate(@ApiParam("平台:[1:ios,2:android]") String os, @ApiParam("版本名称") String versionName) {
		LambdaQueryWrapper<App> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(App::getOs, os)
				.ge(App::getVersionName, versionName)
				.eq(App::getIsOnline,true)
				.orderByDesc(App::getVersionName);
		List<App> appList = appService.list(queryWrapper);
		if (appList != null && !appList.isEmpty()) {
			return R.ok(appList.get(0));
		} else {
			return R.ok(null, "已经是最新版本!");
		}
	}

	@ApiOperation(value = "获取最新版本", tags = {"App版本管理-移动端"})
	@SysLog("获取最新版本")
	@GetMapping("/latest")
	@Inner(value = false)
	public R<LatestVersion> latest(@ApiParam(value = "平台:[1:ios,2:android]",
			required = true) @RequestParam(value = "os") String os) {
		return R.ok(appService.latest(os));
	}

	/**
	 * 通过id删除app管理
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "App版本删除", tags = {"App版本管理-PC端"})
	@SysLog("通过id删除app管理")
	@DeleteMapping("/{id}")
	public R<Boolean> removeById(@PathVariable Long id) {
		return R.ok(appService.removeById(id));
	}

	/**
	 * 版本历史分页查询
	 *
	 * @param page 分页对象
	 * @param os   操作系统
	 * @return R<Page < App>>
	 */
	@ApiOperation(value = "版本历史分页查询", tags = {"App版本管理-移动端"})
	@GetMapping("/history/page")
	@Inner(value = false)
	public R<Page<App>> versionHistory(
			Page<App> page,
			@ApiParam(value = "操作系统:[1:ios,2:android]", required = true) @RequestParam(value = "os") Integer os) {
		LambdaQueryWrapper<App> queryWrapper = Wrappers.lambdaQuery();
		if (os != null && os != 0) {
			queryWrapper.eq(App::getOs, os);
		}
		queryWrapper.orderByDesc(App::getVersionCode);
		return R.ok(appService.page(page, queryWrapper));
	}

}
